require('js/lib/require_patch').monkeypatch(this); // Needed for Android

exports.Util = function() {
	var instance = new Object();
	
	instance.urls = function() {
		var urls = [];
		
		urls['go'] = 'images/go.png';
		urls['go_off'] = 'images/go_off.png';
		urls['stop'] = 'images/stop.png';
		urls['stop_off'] = 'images/stop_off.png';
		urls['yield'] = 'images/yield.png';
		urls['yield_off'] = 'images/yield_off.png';
		urls['bg_gradient'] = 'images/bg_gradient.png';
		urls['bg_clear'] = 'images/bg_clear.png';
		urls['bg_back'] = 'images/bg_back.png';
		urls['bg_next'] = 'images/bg_next.png';
		urls['bg_home'] = 'images/bg_home.png';
		urls['bg_toolbar'] = 'images/bg_toolbar.png';
		urls['none'] = 'images/none.png';
		urls['star'] = 'images/star.png';
		urls['add'] = 'images/add.png';
		urls['slideOut'] = 'images/add_48.png';
		urls['slideIn'] = 'images/cancel_48.png';
	
		return urls;
	};

	instance.images = function() {
		var images = [];
		var urls = globals.util.urls();
		
		images['go'] = image({image:urls['go']}).toBlob();
		images['go_off'] = image({image:urls['go_off']}).toBlob();
		images['stop'] = image({image:urls['stop']}).toBlob();
		images['stop_off'] = image({image:urls['stop_off']}).toBlob();
		images['yield'] = image({image:urls['yield']}).toBlob();
		images['yield_off'] = image({image:urls['yield_off']}).toBlob();
		images['bg_gradient'] = image({image:urls['bg_gradient']}).toBlob();
		images['bg_clear'] = image({image:urls['bg_clear']}).toBlob();
		images['none'] = image({image:urls['none']}).toBlob();
		
		return images;
	};
	
	// Save some typing
	function image(args) {
		return Ti.UI.createImageView(args);
	}

	instance.models = function() {
		return {
			NoteCategory: require('js/model/NoteCategory'),
			Patient: require('js/model/Patient'),
			PatientAppointment: require('js/model/PatientAppointment'),
			PatientNote: require('js/model/PatientNote'),
			PatientTest: require('js/model/PatientTest'),
			Provider: require('js/model/Provider'),
			Test: require('js/model/Test'),
			TestCategory: require('js/model/TestCategory')
		}
	};

	//extend on object with the properties of one or more others (thanks, Dojo!)
	var empty = {};
	function mixin(target,source) {
		var name, s, i;
		for(name in source) {
	
			if (source.hasOwnProperty(name)) {
				s = source[name];
				if(!(name in target) || (target[name] !== s && (!(name in empty) || empty[name] !== s))) {
					target[name] = s;
				}
			}
		}
		return target; // Object
	}

	instance.objectMerge = function(obj,props) {
		if(!obj) {
			obj = {};
		}
		for(var i=1, l=arguments.length; i<l; i++) {
			mixin(obj, arguments[i]);
		}
		return obj; // Object
	};

	// Used to size visual artifacts
	instance.deviceFactor = function() {
		if (Ti.Platform.osname === 'iphone')
			return 1;
		else if (Ti.Platform.osname === 'android')
			return 1.6;
		else
			return 2.2;
	};

	// Converts a date in the format yyyy-mm-dd hh:mm:ss to a date object
	instance.getDateFromStr = function(dateStr) {
		Ti.API.debug('Converting ' + dateStr);
	
		var year = dateStr.substr(0,4), month = dateStr.substr(5,2), day = dateStr.substr(8,2), hour = dateStr.substr(11,2), minute = dateStr.substr(14,2), second = dateStr.substr(17,2);
		Ti.API.debug('Year: '+year+' Month: '+month+' Day: '+day+' Hour: '+hour+' Minute: '+minute+' Second: '+second);
		return new Date(year, month-1, day, hour, minute, second); // Month is relative to zero
	};

	// Debug dump an Object
	instance.debugDump = function(label, params) {
		var paramStr = '';
		for (var key in params) {
			paramStr += ' ' + key + ': ' + params[key];
		}
		
		Ti.API.debug('+=+=+ '+ label +' Parameters are: ' + paramStr);
	};

	return instance;
}